import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hc_bot_kit/service/service.dart';
import 'package:hc_bot_kit/ui/guide_setting/widgets/poi_list.dart';
import 'package:hc_bot_pro/common/service/channel/channel.dart';
import 'package:hc_bot_pro/common/service/sdk_service.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../guide/view.dart';

class SettingController extends GetxController {
  final title = "系统设置";
  String version = "";
  String host = "";

  SettingController();

  _initData() async {
    await prepare();
    update(["setting"]);
  }

  // @override
  // void onInit() {
  //   super.onInit();
  // }

  @override
  void onReady() {
    super.onReady();
    _initData();
  }

  // @override
  // void onClose() {
  //   super.onClose();
  // }

  void testGuide() async {
    await Get.find<RobotHardwareService>().connect();
    await Future.delayed(2.seconds);
    final poiList = await Get.find<RobotHardwareService>().getAllPoi();
    debugPrint(poiList.toString());
    final poi = await Get.to(PoiListPage(poiList: poiList));
    Get.dialog(GuidePage(), arguments: poi, barrierDismissible: false);
  }

  void changeWelcomPhase() async {
    var phase = "";
    await Get.defaultDialog(
      middleText: "Input phase",
      onConfirm: () async {
        if (phase.isEmpty) {
          return;
        }
        Get.back();
      },
      onCancel: () {
        phase = "";
      },
      content: TextField(
        autofocus: true,
        onChanged: (value) {
          phase = value;
        },
      ),
    );
    if (phase.isEmpty) {
      return;
    }
    await Get.find<ChannelService>().saveWelcomePhase(phase);
  }
}

extension on SettingController {
  Future prepare() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    version = "${packageInfo.version}+${packageInfo.buildNumber}";
    //
    host = Get.find<SDKService>().host;
    //
  }
}
